package cn.schoolwow.util.test.module.query.dictionary.testcase;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.check.instance.CheckInstance;
import cn.schoolwow.util.domain.query.dictionary.QueryDictionary;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Assert;

public class QueryDictionaryTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        replaceAnalyzeWorkItem(flowContext);
        replaceArray(flowContext);
        coreAnalyzeTest(flowContext);
        replaceDictionary(flowContext);
    }

    @Override
    public String name() {
        return "查询字典测试用例";
    }

    private void replaceAnalyzeWorkItem(FlowContext flowContext){
        JSONObject dataObject = new JSONObject();
        dataObject.put("compareType",0);
        QueryDictionary.newQuery()
                .addDictionary("compareType", "0:数值,1:文字")
                .setDataObject(dataObject)
                .execute()
                .replaceToDictionaryValue();
        CheckInstance.newCheck()
                .fieldValueCheck(dataObject.getString("compareType"), "数值", "字典替换成值失败");
    }

    private void replaceArray(FlowContext flowContext){
        JSONArray dataArray = new JSONArray();
        for(int i=0;i<4;i++){
            JSONObject dataObject = new JSONObject();
            dataObject.put("workFlowInstanceStatus", i);
            dataArray.add(dataObject);
        }

        QueryDictionary.newQuery()
                .addDictionary("workFlowInstanceStatus", "0:进行中,1:已完成,2:已撤回,3:已拒绝")
                .setDataArray(dataArray)
                .execute()
                .replaceToDictionaryValue();
        for(int i=0;i<dataArray.size();i++){
            JSONObject dataObject = dataArray.getJSONObject(i);
            String workFlowInstanceStatus = dataObject.getString("workFlowInstanceStatus");
            switch (i){
                case 0:{
                    CheckInstance.newCheck().fieldValueCheck("进行中", workFlowInstanceStatus,"字典替换");
                }break;
                case 1:{
                    CheckInstance.newCheck().fieldValueCheck("已完成", workFlowInstanceStatus,"字典替换");
                }break;
                case 2:{
                    CheckInstance.newCheck().fieldValueCheck("已撤回", workFlowInstanceStatus,"字典替换");
                }break;
                case 3:{
                    CheckInstance.newCheck().fieldValueCheck("已拒绝", workFlowInstanceStatus,"字典替换");
                }break;
            }
        }
    }

    private void coreAnalyzeTest(FlowContext flowContext){
        JSONObject dataObject = new JSONObject();
        dataObject.put("coreAnalyze", "是");
        QueryDictionary.newQuery()
                .addDictionary("coreAnalyze", "true:是,false:否")
                .setDataObject(dataObject)
                .execute()
                .replaceToDictionaryKey();
        CheckInstance.newCheck()
                .fieldValueCheck(dataObject.getBooleanValue("coreAnalyze"), true, "是否核心分析布尔值转换失败");
    }

    private void replaceDictionary(FlowContext flowContext){
        JSONArray dataArray = new JSONArray();
        for(int i=0;i<4;i++){
            JSONObject dataObject = new JSONObject();
            dataObject.put("workFlowInstanceStatus", i);
            dataArray.add(dataObject);
        }

        QueryDictionary.newQuery()
                .addReplaceDictionary("workFlowInstanceStatus", "workFlowInstanceStatusString","0:进行中,1:已完成,2:已撤回,3:已拒绝")
                .setDataArray(dataArray)
                .execute()
                .replaceToDictionaryValue();
        for(int i=0;i<dataArray.size();i++){
            JSONObject dataObject = dataArray.getJSONObject(i);
            int workFlowInstanceStatus = dataObject.getIntValue("workFlowInstanceStatus");
            Assert.assertEquals(workFlowInstanceStatus, i);
            String workFlowInstanceStatusString = dataObject.getString("workFlowInstanceStatusString");
            switch (i){
                case 0:{
                    CheckInstance.newCheck().fieldValueCheck("进行中", workFlowInstanceStatusString,"字典替换");
                }break;
                case 1:{
                    CheckInstance.newCheck().fieldValueCheck("已完成", workFlowInstanceStatusString,"字典替换");
                }break;
                case 2:{
                    CheckInstance.newCheck().fieldValueCheck("已撤回", workFlowInstanceStatusString,"字典替换");
                }break;
                case 3:{
                    CheckInstance.newCheck().fieldValueCheck("已拒绝", workFlowInstanceStatusString,"字典替换");
                }break;
            }
        }
    }
}
